home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / macabuse / src / net / mac / mactcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-20  |  4.7 KB  |  196 lines

  1. #include "tcpip.hpp"
  2. #include <ctype.h>
  3.  
  4. tcpip_protocol tcpip;
  5.  
  6. /*
  7. FILE *log_file=NULL;
  8. extern int net_start();
  9. void net_log(char *st, void *buf, long size)
  10. {
  11.     
  12.   if (!log_file) 
  13.   {
  14.     if (net_start())
  15.       log_file=fopen("client.log","wb");
  16.     else
  17.       log_file=fopen("server.log","wb");
  18.   }
  19.  
  20.  
  21.     fprintf(log_file,"%s%d - ",st,size);
  22.     int i;
  23.     for (i=0;i<size;i++) 
  24.       if (isprint(*((unsigned char *)buf+i)))
  25.         fprintf(log_file,"%c",*((unsigned char *)buf+i));
  26.       else fprintf(log_file,"~");
  27.  
  28.     fprintf(log_file," : ");
  29.  
  30.     for (i=0;i<size;i++) 
  31.     fprintf(log_file,"%02x, ",*((unsigned char *)buf+i),*((unsigned char *)buf+i));
  32.     fprintf(log_file,"\n");
  33.     fflush(log_file);
  34.  
  35. } */
  36.  
  37.  
  38. int unix_fd::read(void *buf, int size, net_address **addr) 
  39. {
  40.   int tr=::read(fd,buf,size);
  41.   if (addr) *addr=NULL;
  42.   return tr;
  43. }
  44.  
  45. int unix_fd::write(void *buf, int size, net_address *addr)
  46.   if (addr) fprintf(stderr,"Cannot change address for this socket type\n");
  47.   return ::write(fd,buf,size); 
  48. }
  49.  
  50.  
  51. net_address *tcpip_protocol::get_local_address()
  52. {
  53.   char my_name[100];                              // now check to see if this address is 'hostname'
  54.   gethostname(my_name,100);
  55.   struct hostent *l_hn=gethostbyname(my_name);  
  56.   ip_address *a=new ip_address();
  57.   memset(&a->addr,0,sizeof(a->addr));
  58.   memcpy(&a->addr.sin_addr,*l_hn->h_addr_list,4);  
  59. }
  60.  
  61. net_address *tcpip_protocol::get_node_address(char *&server_name, int def_port, int force_port)
  62. {
  63.   char name[256],*np;
  64.   np=name;
  65.   while (*server_name && *server_name!=':' && *server_name!='/')
  66.     *(np++)=*(server_name)++;
  67.   *np=0;
  68.   if (*server_name==':')
  69.   {
  70.     server_name++;
  71.     char port[256],*p;
  72.     p=port;
  73.     while (*server_name && *server_name!='/')
  74.       *(p++)=*(server_name++);
  75.     *p=0;
  76.     int x;
  77.     if (!force_port)
  78.     {
  79.       if (sscanf(port,"%d",&x)==1) def_port=x;
  80.       else return 0;
  81.     }
  82.   }
  83.  
  84.   if (*server_name=='/') server_name++;
  85.  
  86.   hostent *hp=gethostbyname(name);
  87.   if (!hp)
  88.   { 
  89.     fprintf(stderr,"unable to locate server named '%s'\n",name);
  90.     return 0;
  91.   }
  92.   
  93.  
  94.   sockaddr_in host;
  95.   memset( (char*) &host,0, sizeof(host));
  96.   host.sin_family = AF_INET;
  97.   host.sin_port = htons(def_port);
  98.   host.sin_addr.s_addr = htonl(INADDR_ANY);
  99.   memcpy(&host.sin_addr,hp->h_addr,hp->h_length);
  100.   return new ip_address(&host);
  101. }
  102.  
  103. net_socket *tcpip_protocol::connect_to_server(net_address *addr, net_socket::socket_type sock_type)
  104. {
  105.   if (addr->protocol_type()!=net_address::IP)
  106.   {
  107.     fprintf(stderr,"Procotol type not supported in the executable\n");
  108.     return NULL;
  109.   }
  110.  
  111.   int socket_fd=socket(AF_INET,sock_type==net_socket::SOCKET_SECURE ? SOCK_STREAM : SOCK_DGRAM,0);
  112.   if (socket_fd<0) 
  113.   {
  114.     fprintf(stderr,"unable to create socket (too many open files?)\n");
  115.     return 0;
  116.   }
  117.  
  118.   int zz;
  119.   if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&zz,sizeof(zz))<0)
  120.   {
  121.     fprintf(stderr,"could not set socket option reuseaddr");
  122.     return 0;
  123.   }
  124.  
  125.     
  126.   if (connect(socket_fd, (struct sockaddr *) &((ip_address *)addr)->addr, sizeof( ((ip_address *)addr)->addr ))==-1)
  127.   { 
  128.     fprintf(stderr,"unable to connect\n");
  129.     close(socket_fd);
  130.     return 0;
  131.   }
  132.  
  133.   if (sock_type==net_socket::SOCKET_SECURE)
  134.     return new tcp_socket(socket_fd);
  135.   else
  136.     return new udp_socket(socket_fd);
  137. }
  138.  
  139.  
  140. net_socket *tcpip_protocol::create_listen_socket(int port, net_socket::socket_type sock_type, char *name)
  141. {
  142.   int socket_fd=socket(AF_INET,sock_type==net_socket::SOCKET_SECURE ? SOCK_STREAM : SOCK_DGRAM,0);
  143.   if (socket_fd<0) 
  144.   {
  145.     fprintf(stderr,"unable to create socket (too many open files?)\n");
  146.     return 0;
  147.   }
  148. /*  int zz;
  149.   if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&zz,sizeof(zz))<0)
  150.   {
  151.     fprintf(stderr,"could not set socket option reuseaddr");
  152.     return 0;
  153.   } */
  154.  
  155.  
  156.   net_socket *s;
  157.   if (sock_type==net_socket::SOCKET_SECURE)
  158.     s=new tcp_socket(socket_fd);
  159.   else s=new udp_socket(socket_fd);
  160.   if (s->listen(port)==0)
  161.   {   
  162.     delete s;
  163.     return 0;
  164.   }
  165.  
  166.   return s;
  167. }
  168.  
  169.  
  170. tcpip_protocol::tcpip_protocol()
  171. {
  172.   FD_ZERO(&master_set);  
  173.   FD_ZERO(&master_write_set);  
  174.   FD_ZERO(&read_set);
  175.   FD_ZERO(&exception_set);
  176.   FD_ZERO(&write_set); 
  177. }
  178.  
  179.  
  180. int tcpip_protocol::select(int block)
  181. {
  182.   memcpy(&read_set,&master_set,sizeof(master_set));
  183.   memcpy(&exception_set,&master_set,sizeof(master_set));
  184.   memcpy(&write_set,&master_write_set,sizeof(master_set));
  185.   if (block)
  186.     return ::select(FD_SETSIZE,&read_set,&write_set,&exception_set,NULL);
  187.   else
  188.   {
  189.     timeval tv={0,0};
  190.     return ::select(FD_SETSIZE,&read_set,&write_set,&exception_set,&tv);
  191.   }
  192. }
  193.  
  194.  
  195.